package halma;

public class Game {
	private ChessBoard chessBoard = null;
	private int numberOfPlayers = 0;
	private int currentPlayer = 0;
	private ChessBoardUnit[][] chessBoardUnits;
	private EasyAi[] easyAis;

	public ChessBoard getChessBoard() {
		return chessBoard;
	}

	public void setChessBoard(ChessBoard chessBoard) {
		this.chessBoard = chessBoard;
	}

	public Game(int numberOfPlayers) {
		currentPlayer = 1;
		chessBoard = new ChessBoard(numberOfPlayers);
		this.numberOfPlayers = numberOfPlayers;
		chessBoardUnits = chessBoard.getChessBoardUnits();
		switch (numberOfPlayers) {
		case 2:
			easyAis = new EasyAi[2];
			easyAis[0] = new EasyAi(Director.up, this);
			easyAis[1] = new EasyAi(Director.down, this);
			break;
		case 3:
			break;

		case 4:
			easyAis = new EasyAi[4];
			easyAis[0] = new EasyAi(Director.leftUp, this);
			easyAis[1] = new EasyAi(Director.rightUp, this);
			easyAis[2] = new EasyAi(Director.rightDown, this);
			easyAis[3] = new EasyAi(Director.leftDown, this);
			System.out.println("Game:��ʼ��4�������˳ɹ�");
			break;
		case 6:
			easyAis = new EasyAi[6];
			easyAis[0] = new EasyAi(Director.leftUp, this);
			easyAis[1] = new EasyAi(Director.up, this);
			easyAis[2] = new EasyAi(Director.rightUp, this);
			easyAis[3] = new EasyAi(Director.rightDown, this);
			easyAis[4] = new EasyAi(Director.down, this);
			easyAis[5] = new EasyAi(Director.leftDown, this);
			break;
		default:
			System.out.println("Game:�����˳�ʼ��ʧ��");
		}
	}

	// ��start �ƶ���end �������ƶ����ƶ�������true
	public boolean move(Coordinate start,Coordinate end) {
		int startX=start.getX();
		int startY=start.getY();
		int endX=end.getX();
		int endY=end.getY();
		if (chessBoardUnits[startX][startY] == null || chessBoardUnits[endX][endY] == null) {
			System.out.println("��񲻴���");
			return false;
		}
		if (chessBoardUnits[startX][startY].getBall() == null || chessBoardUnits[endX][endY].getBall() != null) {
			System.out.println("��������ӻ��յ�������");
			return false;
		} else {
			int diffX = endX - startX;
			int diffY = endY - startY;
			// ��һ������
			if ((Math.abs(diffX) == 1 || diffX == 0) && (Math.abs(diffY) == 1 || diffY == 0)) {
				System.out.println("��һ�񽻻��ɹ�");
				chessBoardUnits[endX][endY].setBall(chessBoardUnits[startX][startY].getBall());
				chessBoardUnits[startX][startY].setBall(null);

			}
			// ����������
			else if ((Math.abs(diffX) == 2 || diffX == 0) && (Math.abs(diffY) == 2 || diffY == 0)) {
				if (chessBoardUnits[endX / 2][endY / 2].getBall() == null) {
					System.out.println("ȱ����Ծ������");
					return false;
				} else {
					chessBoardUnits[endX][endY].setBall(chessBoardUnits[startX][startY].getBall());
					chessBoardUnits[startX][startY].setBall(null);
					System.out.println("������ɹ�");
				}
			}
			// �޷��ߵ����
			else {
				System.out.println("�������Ӳ���һ��ֱ����");
				return false;
			}
		}
		return true;
	}

	// ������Ҫ���л����˵ķ���
	Coordinate[] runAi(int type) {
		EasyAi ea = null;
		for (int i = 0; i < easyAis.length; i++) {
			// System.out.println(easyAis.length);
			if ((int) easyAis[i].getType() == (int) type) {
				ea = easyAis[i];
				break;
			}
		}
		if (ea == null)
			System.out.println("Game:δ�ҵ�������");
		else {
			ea.checkAll();
			return ea.select();
		}
		return null;
	}

	// �Գ�������ж�ʤ��
	boolean isWin(int type) {
		switch (type) {
		case 1:
			for (int x = 4; x < 8; x++)
				for (int y = 9; y < 13; y++) {
					if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.leftUp) {
						if (chessBoardUnits[x][y].getBall().getType() == Director.rightDown)
							continue;
						else {
							System.out.println("δʤ����" + type);
							return false;
						}
					}
				}
			System.out.println("ʤ����" + type);
			return true;

		case 2:
			for (int x = 9; x < 13; x++)
				for (int y = 13; y < 17; y++) {
					if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.up) {
						if (chessBoardUnits[x][y].getBall().getType() == Director.down)
							continue;
						else {
							System.out.println("δʤ����" + type);
							return false;
						}
					}
				}

			System.out.println("ʤ����" + type);
			return true;
		case 3:
			for (int x = 13; x < 17; x++)
				for (int y = 9; y < 13; y++) {
					if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.rightUp) {
						if (chessBoardUnits[x][y].getBall().getType() == Director.leftDown)
							continue;
						else {
							System.out.println("δʤ����" + type);
							return false;
						}
					}
				}
			System.out.println("ʤ����" + type);
			return true;
		case 5:
			for (int x = 9; x < 13; x++)
				for (int y = 4; y < 8; y++) {
					if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.rightDown) {
						if (chessBoardUnits[x][y].getBall().getType() == Director.leftUp)
							continue;
						else {
							System.out.println("δʤ����" + type);
							return false;
						}
					}
				}
			System.out.println("ʤ����" + type);
			return true;
		case 6:
			for (int x = 4; x < 8; x++)
				for (int y = 0; y < 4; y++) {
					if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.down) {
						if (chessBoardUnits[x][y].getBall().getType() == Director.up)
							continue;
						else {
							System.out.println("δʤ����" + type);
							return false;
						}
					}
				}
			System.out.println("ʤ����" + type);
			return true;
		case 7:
			for (int x = 0; x < 4; x++)
				for (int y = 4; y < 8; y++) {
					if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.leftDown) {
						if (chessBoardUnits[x][y].getBall().getType() == Director.rightUp)
							continue;
						else {
							System.out.println("δʤ����" + type);
							return false;
						}
					}
				}
			System.out.println("ʤ����" + type);
			return true;

		default:
			System.out.println("isWin:�������");
			return false;
		}
	}

	// �����ж�ʤ��
	boolean isWin(int type, int numberOfPlayers) {
		if (numberOfPlayers == 3) {
			switch (type) {
			case 2:
				for (int x = 9; x < 13; x++)
					for (int y = 13; y < 17; y++) {
						if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.up) {
							if (chessBoardUnits[x][y].getBall().getType() == Director.leftDown)
								continue;
							else
								return false;
						}
					}
				return true;
			case 5:
				for (int x = 9; x < 13; x++)
					for (int y = 4; y < 8; y++) {
						if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.rightDown) {
							if (chessBoardUnits[x][y].getBall().getType() == Director.up)
								continue;
							else
								return false;
						}
					}
				return true;
			case 7:
				for (int x = 0; x < 4; x++)
					for (int y = 4; y < 8; y++) {
						if (chessBoardUnits[x][y] != null && chessBoardUnits[x][y].getType() == Director.leftDown) {
							if (chessBoardUnits[x][y].getBall().getType() == Director.rightDown)
								continue;
							else
								return false;
						}
					}
				return true;

			default:
				System.out.println("isWin:�������");
				return false;
			}

		} else
			return isWin(type);
	}

	void display() {
		ChessBoardUnit[][] chessBoardUnits = chessBoard.getChessBoardUnits();
		int x, y;
		int Sum = 0;
		int sum[] = new int[9];
		for (int k = 0; k < 9; k++) {
			sum[k] = 0;
		}
		for (int i = 0; i < 17; i++)
			for (int j = 0; j < 17; j++) {
				if (chessBoardUnits[i][j] != null) {
					Sum++;
					x = chessBoardUnits[i][j].getX() + 1;
					y = chessBoardUnits[i][j].getY() + 1;
					sum[chessBoardUnits[i][j].getType()]++;
					System.out.print("��" + Sum + "��:    " + "x:" + x + "   y:" + y + "   type:"
							+ chessBoardUnits[i][j].getType());
					if (chessBoardUnits[i][j].getBall() != null)
						System.out.println("   ballType:" + chessBoardUnits[i][j].getBall().getType() + "   ballNum:"
								+ chessBoardUnits[i][j].getBall().getNumber());
					else
						System.out.println("");
				}
			}
		for (int k = 0; k < 9; k++) {
			System.out.println("sum" + k + ":" + sum[k]);
		}
		System.out.println("Sum:" + Sum);

	}
}
